package com.alibaba.fastjson.util;

import com.google.android.exoplayer2.C;
import com.tencent.bugly.beta.tinker.TinkerReport;
import java.lang.reflect.Array;
import java.math.BigInteger;

/* loaded from: classes.dex */
public final class RyuDouble {
    private static final int[][] POW5_SPLIT = (int[][]) Array.newInstance(int.class, 326, 4);
    private static final int[][] POW5_INV_SPLIT = (int[][]) Array.newInstance(int.class, 291, 4);

    static {
        BigInteger subtract = BigInteger.ONE.shiftLeft(31).subtract(BigInteger.ONE);
        BigInteger subtract2 = BigInteger.ONE.shiftLeft(31).subtract(BigInteger.ONE);
        int i = 0;
        while (i < 326) {
            BigInteger pow = BigInteger.valueOf(5L).pow(i);
            int bitLength = pow.bitLength();
            int i2 = i == 0 ? 1 : (int) ((((i * 23219280) + 10000000) - 1) / 10000000);
            if (i2 != bitLength) {
                throw new IllegalStateException(bitLength + " != " + i2);
            }
            if (i < POW5_SPLIT.length) {
                for (int i3 = 0; i3 < 4; i3++) {
                    POW5_SPLIT[i][i3] = pow.shiftRight((bitLength - 121) + ((3 - i3) * 31)).and(subtract).intValue();
                }
            }
            if (i < POW5_INV_SPLIT.length) {
                BigInteger add = BigInteger.ONE.shiftLeft(bitLength + TinkerReport.KEY_APPLIED_DEXOPT_OTHER).divide(pow).add(BigInteger.ONE);
                for (int i4 = 0; i4 < 4; i4++) {
                    if (i4 == 0) {
                        POW5_INV_SPLIT[i][i4] = add.shiftRight((3 - i4) * 31).intValue();
                    } else {
                        POW5_INV_SPLIT[i][i4] = add.shiftRight((3 - i4) * 31).and(subtract2).intValue();
                    }
                }
            }
            i++;
        }
    }

    public static String toString(double d) {
        char[] cArr = new char[24];
        return new String(cArr, 0, toString(d, cArr, 0));
    }

    public static int toString(double d, char[] cArr, int i) {
        int i2;
        boolean z;
        boolean z2;
        long j;
        long j2;
        long j3;
        int i3;
        boolean z3;
        boolean z4;
        long j4;
        int i4;
        int i5;
        int i6;
        int i7;
        int i8;
        int i9;
        boolean z5;
        int i10;
        int i11;
        int i12;
        int i13;
        if (!Double.isNaN(d)) {
            if (d == Double.POSITIVE_INFINITY) {
                int i14 = i + 1;
                cArr[i] = 'I';
                int i15 = i14 + 1;
                cArr[i14] = 'n';
                int i16 = i15 + 1;
                cArr[i15] = 'f';
                int i17 = i16 + 1;
                cArr[i16] = 'i';
                int i18 = i17 + 1;
                cArr[i17] = 'n';
                int i19 = i18 + 1;
                cArr[i18] = 'i';
                int i20 = i19 + 1;
                cArr[i19] = 't';
                i9 = i20 + 1;
                cArr[i20] = 'y';
            } else if (d == Double.NEGATIVE_INFINITY) {
                int i21 = i + 1;
                cArr[i] = '-';
                int i22 = i21 + 1;
                cArr[i21] = 'I';
                int i23 = i22 + 1;
                cArr[i22] = 'n';
                int i24 = i23 + 1;
                cArr[i23] = 'f';
                int i25 = i24 + 1;
                cArr[i24] = 'i';
                int i26 = i25 + 1;
                cArr[i25] = 'n';
                int i27 = i26 + 1;
                cArr[i26] = 'i';
                int i28 = i27 + 1;
                cArr[i27] = 't';
                i13 = i28 + 1;
                cArr[i28] = 'y';
            } else {
                long doubleToLongBits = Double.doubleToLongBits(d);
                if (doubleToLongBits == 0) {
                    int i29 = i + 1;
                    cArr[i] = '0';
                    int i30 = i29 + 1;
                    cArr[i29] = '.';
                    i13 = i30 + 1;
                    cArr[i30] = '0';
                } else if (doubleToLongBits == Long.MIN_VALUE) {
                    int i31 = i + 1;
                    cArr[i] = '-';
                    int i32 = i31 + 1;
                    cArr[i31] = '0';
                    int i33 = i32 + 1;
                    cArr[i32] = '.';
                    i9 = i33 + 1;
                    cArr[i33] = '0';
                } else {
                    int i34 = (int) ((doubleToLongBits >>> 52) & 2047);
                    long j5 = 4503599627370495L & doubleToLongBits;
                    if (i34 == 0) {
                        i2 = -1074;
                    } else {
                        i2 = (i34 - 1023) - 52;
                        j5 |= 4503599627370496L;
                    }
                    boolean z6 = doubleToLongBits < 0;
                    boolean z7 = (j5 & 1) == 0;
                    long j6 = 4 * j5;
                    long j7 = j6 + 2;
                    int i35 = (j5 != 4503599627370496L || i34 <= 1) ? 1 : 0;
                    long j8 = (j6 - 1) - i35;
                    int i36 = i2 - 2;
                    if (i36 >= 0) {
                        int max = Math.max(0, ((int) ((i36 * 3010299) / 10000000)) - 1);
                        int i37 = ((((-i36) + max) + (((max == 0 ? 1 : (int) ((((max * 23219280) + 10000000) - 1) / 10000000)) + 122) - 1)) - 93) - 21;
                        if (i37 < 0) {
                            throw new IllegalArgumentException("" + i37);
                        }
                        int[] iArr = POW5_INV_SPLIT[max];
                        long j9 = j6 >>> 31;
                        long j10 = j6 & 2147483647L;
                        z = z6;
                        z2 = z7;
                        long j11 = ((((((((((((j10 * iArr[3]) >>> 31) + (iArr[2] * j10)) + (j9 * iArr[3])) >>> 31) + (iArr[1] * j10)) + (iArr[2] * j9)) >>> 31) + (iArr[0] * j10)) + (iArr[1] * j9)) >>> 21) + ((iArr[0] * j9) << 10)) >>> i37;
                        long j12 = j7 >>> 31;
                        long j13 = j7 & 2147483647L;
                        long j14 = ((((((((((((j13 * iArr[3]) >>> 31) + (iArr[2] * j13)) + (j12 * iArr[3])) >>> 31) + (iArr[1] * j13)) + (iArr[2] * j12)) >>> 31) + (iArr[0] * j13)) + (iArr[1] * j12)) >>> 21) + ((iArr[0] * j12) << 10)) >>> i37;
                        long j15 = j8 >>> 31;
                        long j16 = j8 & 2147483647L;
                        j2 = j14;
                        j3 = ((((((((((((j16 * iArr[3]) >>> 31) + (iArr[2] * j16)) + (j15 * iArr[3])) >>> 31) + (iArr[1] * j16)) + (iArr[2] * j15)) >>> 31) + (iArr[0] * j16)) + (iArr[1] * j15)) >>> 21) + ((iArr[0] * j15) << 10)) >>> i37;
                        if (max <= 21) {
                            long j17 = j6 % 5;
                            if (j17 == 0) {
                                if (j17 != 0) {
                                    i12 = 0;
                                } else if (j6 % 25 != 0) {
                                    i12 = 1;
                                } else if (j6 % 125 != 0) {
                                    i12 = 2;
                                } else if (j6 % 625 != 0) {
                                    i12 = 3;
                                } else {
                                    long j18 = j6 / 625;
                                    i12 = 4;
                                    for (long j19 = 0; j18 > j19 && j18 % 5 == j19; j19 = 0) {
                                        j18 /= 5;
                                        i12++;
                                    }
                                }
                                z5 = i12 >= max;
                                z4 = false;
                                z3 = z5;
                                i3 = max;
                                j = j11;
                            } else if (z2) {
                                if (j8 % 5 != 0) {
                                    i11 = 0;
                                } else if (j8 % 25 != 0) {
                                    i11 = 1;
                                } else if (j8 % 125 != 0) {
                                    i11 = 2;
                                } else if (j8 % 625 != 0) {
                                    i11 = 3;
                                } else {
                                    long j20 = j8 / 625;
                                    i11 = 4;
                                    for (long j21 = 0; j20 > j21 && j20 % 5 == j21; j21 = 0) {
                                        j20 /= 5;
                                        i11++;
                                    }
                                }
                                z4 = i11 >= max;
                                z5 = false;
                                z3 = z5;
                                i3 = max;
                                j = j11;
                            } else {
                                if (j7 % 5 != 0) {
                                    i10 = 0;
                                } else if (j7 % 25 != 0) {
                                    i10 = 1;
                                } else if (j7 % 125 != 0) {
                                    i10 = 2;
                                } else if (j7 % 625 != 0) {
                                    i10 = 3;
                                } else {
                                    long j22 = j7 / 625;
                                    i10 = 4;
                                    for (long j23 = 0; j22 > j23 && j22 % 5 == j23; j23 = 0) {
                                        j22 /= 5;
                                        i10++;
                                    }
                                }
                                if (i10 >= max) {
                                    j2--;
                                }
                            }
                        }
                        z5 = false;
                        z4 = false;
                        z3 = z5;
                        i3 = max;
                        j = j11;
                    } else {
                        z = z6;
                        z2 = z7;
                        int i38 = -i36;
                        int max2 = Math.max(0, ((int) ((i38 * 6989700) / 10000000)) - 1);
                        int i39 = i38 - max2;
                        int i40 = ((max2 - ((i39 == 0 ? 1 : (int) ((((i39 * 23219280) + 10000000) - 1) / 10000000)) - 121)) - 93) - 21;
                        if (i40 < 0) {
                            throw new IllegalArgumentException("" + i40);
                        }
                        int[] iArr2 = POW5_SPLIT[i39];
                        long j24 = j6 >>> 31;
                        long j25 = j6 & 2147483647L;
                        long j26 = iArr2[0] * j24;
                        long j27 = iArr2[0] * j25;
                        long j28 = iArr2[1] * j24;
                        long j29 = iArr2[1] * j25;
                        int i41 = i35;
                        long j30 = iArr2[2] * j24;
                        long j31 = iArr2[2] * j25;
                        long j32 = j24 * iArr2[3];
                        long j33 = j7 >>> 31;
                        long j34 = j7 & 2147483647L;
                        long j35 = iArr2[0] * j33;
                        j = ((((((((((((j25 * iArr2[3]) >>> 31) + j31) + j32) >>> 31) + j29) + j30) >>> 31) + j27) + j28) >>> 21) + (j26 << 10)) >>> i40;
                        long j36 = ((((((((j34 * iArr2[3]) >>> 31) + (iArr2[2] * j34)) + (iArr2[3] * j33)) >>> 31) + (iArr2[1] * j34)) + (iArr2[2] * j33)) >>> 31) + (iArr2[0] * j34);
                        long j37 = j8 >>> 31;
                        long j38 = j8 & 2147483647L;
                        long j39 = iArr2[0] * j38;
                        long j40 = iArr2[1] * j37;
                        long j41 = iArr2[1] * j38;
                        j2 = (((j36 + (iArr2[1] * j33)) >>> 21) + (j35 << 10)) >>> i40;
                        j3 = ((((((((((((j38 * iArr2[3]) >>> 31) + (iArr2[2] * j38)) + (j37 * iArr2[3])) >>> 31) + j41) + (iArr2[2] * j37)) >>> 31) + j39) + j40) >>> 21) + ((iArr2[0] * j37) << 10)) >>> i40;
                        i3 = max2 + i36;
                        z3 = true;
                        if (max2 <= 1) {
                            if (!z2) {
                                j2--;
                            } else if (i41 == 1) {
                                z4 = true;
                            }
                        } else if (max2 < 63) {
                            z3 = (j6 & ((1 << (max2 - 1)) - 1)) == 0;
                        } else {
                            z3 = false;
                        }
                        z4 = false;
                    }
                    int i42 = j2 >= 1000000000000000000L ? 19 : j2 >= 100000000000000000L ? 18 : j2 >= 10000000000000000L ? 17 : j2 >= 1000000000000000L ? 16 : j2 >= 100000000000000L ? 15 : j2 >= 10000000000000L ? 14 : j2 >= 1000000000000L ? 13 : j2 >= 100000000000L ? 12 : j2 >= 10000000000L ? 11 : j2 >= C.NANOS_PER_SECOND ? 10 : j2 >= 100000000 ? 9 : j2 >= 10000000 ? 8 : j2 >= 1000000 ? 7 : j2 >= 100000 ? 6 : j2 >= 10000 ? 5 : j2 >= 1000 ? 4 : j2 >= 100 ? 3 : j2 >= 10 ? 2 : 1;
                    int i43 = (i3 + i42) - 1;
                    boolean z8 = i43 < -3 || i43 >= 7;
                    if (z4 || z3) {
                        int i44 = 0;
                        int i45 = 0;
                        while (true) {
                            long j42 = j2 / 10;
                            long j43 = j3 / 10;
                            if (j42 <= j43 || (j2 < 100 && z8)) {
                                break;
                            }
                            z4 &= j3 % 10 == 0;
                            z3 &= i44 == 0;
                            i44 = (int) (j % 10);
                            j /= 10;
                            i45++;
                            j2 = j42;
                            j3 = j43;
                        }
                        if (z4 && z2) {
                            while (j3 % 10 == 0 && (j2 >= 100 || !z8)) {
                                z3 &= i44 == 0;
                                i44 = (int) (j % 10);
                                j2 /= 10;
                                j /= 10;
                                j3 /= 10;
                                i45++;
                            }
                        }
                        if (z3 && i44 == 5 && j % 2 == 0) {
                            i44 = 4;
                        }
                        j4 = j + (((j != j3 || (z4 && z2)) && i44 < 5) ? 0 : 1);
                        i4 = i45;
                    } else {
                        i4 = 0;
                        int i46 = 0;
                        while (true) {
                            long j44 = j2 / 10;
                            long j45 = j3 / 10;
                            if (j44 <= j45 || (j2 < 100 && z8)) {
                                break;
                            }
                            i46 = (int) (j % 10);
                            j /= 10;
                            i4++;
                            j2 = j44;
                            j3 = j45;
                        }
                        j4 = j + ((j == j3 || i46 >= 5) ? 1 : 0);
                    }
                    int i47 = i42 - i4;
                    if (z) {
                        i5 = i + 1;
                        cArr[i] = '-';
                    } else {
                        i5 = i;
                    }
                    if (!z8) {
                        char c = '0';
                        if (i43 < 0) {
                            int i48 = i5 + 1;
                            cArr[i5] = '0';
                            int i49 = i48 + 1;
                            cArr[i48] = '.';
                            int i50 = -1;
                            while (i50 > i43) {
                                cArr[i49] = c;
                                i50--;
                                i49++;
                                c = '0';
                            }
                            i6 = i49;
                            for (int i51 = 0; i51 < i47; i51++) {
                                cArr[((i49 + i47) - i51) - 1] = (char) ((j4 % 10) + 48);
                                j4 /= 10;
                                i6++;
                            }
                        } else {
                            int i52 = i43 + 1;
                            if (i52 >= i47) {
                                for (int i53 = 0; i53 < i47; i53++) {
                                    cArr[((i5 + i47) - i53) - 1] = (char) ((j4 % 10) + 48);
                                    j4 /= 10;
                                }
                                int i54 = i5 + i47;
                                while (i47 < i52) {
                                    cArr[i54] = '0';
                                    i47++;
                                    i54++;
                                }
                                int i55 = i54 + 1;
                                cArr[i54] = '.';
                                i6 = i55 + 1;
                                cArr[i55] = '0';
                            } else {
                                int i56 = i5 + 1;
                                for (int i57 = 0; i57 < i47; i57++) {
                                    if ((i47 - i57) - 1 == i43) {
                                        cArr[((i56 + i47) - i57) - 1] = '.';
                                        i56--;
                                    }
                                    cArr[((i56 + i47) - i57) - 1] = (char) ((j4 % 10) + 48);
                                    j4 /= 10;
                                }
                                i6 = i5 + i47 + 1;
                            }
                        }
                        return i6 - i;
                    }
                    for (int i58 = 0; i58 < i47 - 1; i58++) {
                        int i59 = (int) (j4 % 10);
                        j4 /= 10;
                        cArr[(i5 + i47) - i58] = (char) (i59 + 48);
                    }
                    cArr[i5] = (char) ((j4 % 10) + 48);
                    cArr[i5 + 1] = '.';
                    int i60 = i5 + i47 + 1;
                    if (i47 == 1) {
                        cArr[i60] = '0';
                        i60++;
                    }
                    int i61 = i60 + 1;
                    cArr[i60] = 'E';
                    if (i43 < 0) {
                        i7 = i61 + 1;
                        cArr[i61] = '-';
                        i43 = -i43;
                    } else {
                        i7 = i61;
                    }
                    if (i43 >= 100) {
                        int i62 = i7 + 1;
                        i8 = 48;
                        cArr[i7] = (char) ((i43 / 100) + 48);
                        i43 %= 100;
                        i7 = i62 + 1;
                        cArr[i62] = (char) ((i43 / 10) + 48);
                    } else {
                        i8 = 48;
                        if (i43 >= 10) {
                            cArr[i7] = (char) ((i43 / 10) + 48);
                            i7++;
                        }
                    }
                    i9 = i7 + 1;
                    cArr[i7] = (char) ((i43 % 10) + i8);
                }
            }
            return i9 - i;
        }
        int i63 = i + 1;
        cArr[i] = 'N';
        int i64 = i63 + 1;
        cArr[i63] = 'a';
        i13 = i64 + 1;
        cArr[i64] = 'N';
        return i13 - i;
    }
}
